package com.fan.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fan.reggie.common.BaseContext;
import com.fan.reggie.common.R;
import com.fan.reggie.entity.Employee;
import com.fan.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
   // @Resource
    private EmployeeService employeeService;

    /*员工登录
     *@Param request
     *@Param employee
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        //1.将页提交的密码password进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2，与数据库数据进行匹配
        LambdaQueryWrapper<Employee> queryWarpper = new LambdaQueryWrapper<>();
        queryWarpper.eq(Employee::getUsername, employee.getUsername());
        Employee employee1 = employeeService.getOne(queryWarpper);

        //3.查询不到则返回失败结果
        if(employee1 == null)
            return R.error("登录失败，用户不存在");
        //4.否则，密码比对
        if(!employee1.getPassword().equals(password))
            return R.error("登录失败，密码不正确");
        //5.判断账号是否禁用
        if(employee1.getStatus()==0)
            return R.error("登录失败，账号已禁用");
        //6.登录成功，将员工信息存入session，返回成功信息
        request.getSession().setAttribute("employee", employee1.getId());
       // log.info("emlpoyee数据:{}",request.getSession().getAttribute("employee"));
        return R.success(employee1);
    }

    /**员工退出
     * @Param request
     **/
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清理session中保存的数据
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /*新增员工
     * */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        //设置初始密码123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));*/
        log.info("新增员工信息为；{}",employee.toString());
        employeeService.save(employee);
        return R.success("保存成功");
    }

    /*员工信息分页查询
    * */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){

        //构造分页构造器
        Page pageinfo = new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper();
        //添加过滤条件
        wrapper.like(StringUtils.isNotEmpty(name), Employee::getName,name);
        //添加排序条件
        wrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageinfo,wrapper);
        return R.success(pageinfo);
    }

    /*更新员工数据
    * */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody  Employee employee){
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /*根据id查询员工信息
    * */
    @GetMapping("/{id}")
    public R<Employee> editEmployee(@PathVariable Long id){
        log.info("根据ID查询员工信息....");
        Employee employee = employeeService.getById(id);
        if (employee!=null)
            return R.success(employee);
        else
            return R.error("没有查询到对应员工信息");
    }
}
